package com.xsp.controller;

import com.xsp.entity.CommonResult;
import com.xsp.entity.User;
import com.xsp.service.UserService;
import com.xsp.util.JWTUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xsp
 * @date 2024/3/10
 * @desc
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

//    @RequestMapping("login/{username}/{password}")
//    public CommonResult login(@PathVariable("username") String username,
//                              @PathVariable("password") String password) {
    @RequestMapping("login")
    public CommonResult login(User user) {
        User user1 = userService.find(user.getUsername(), user.getPassword());
//        User user1 = userService.find(username, password);
        if (user1 != null) {
            Map<String, String> params = new HashMap<>();
            params.put("username", user1.getUsername());
            String token = JWTUtil.getToken(params);
            return CommonResult.success(token, "登录验证成功");
        } else {
            return CommonResult.error("用户名或密码有误");
        }
    }


    @RequestMapping("test")
    public CommonResult test() {
        return CommonResult.success("hello。。。");
    }
}
